package com.rui.patterns.singleton.demo08;

import java.lang.reflect.Constructor;

/**
 * @className    : Client
 * @author       : [锐神NO.3]
 * @version      : [v1.0]
 * @createTime   : [2022/2/9 12:30]
 * @description  : [测试使用反射破坏单例模式]
 */
 
public class Client {
    public static void main(String[] args) throws Exception {
        //1.获取Singleton的字节码对象
        Class<Singleton> clazz = Singleton.class;

        //2.获取无参构造方法对象
        Constructor<Singleton> cons = clazz.getDeclaredConstructor();

        //3.取消访问检查
        cons.setAccessible(true);

        //4.创建Singleton对象
        Singleton s1 = cons.newInstance();
        Singleton s2 = cons.newInstance();

        //如果返回的是true 说明并没有破坏单例模式
        System.out.println(s1 == s2);
    }
}
